package controller;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;import java.io.File;import java.io.IOException;import javax.swing.JFileChooser;import model.Prescription;
import view.Canvas;
import view.Designer;
import view.Toolbox;
import io.ExportCSS;import io.ExportImage;import io.ExportXML;
import io.FileExtensionUtils;import io.FileSaveDialog;import io.ImportXML;/** * An event handler class for the Input and Output set of buttons at the bottom of the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
public class IOPanelEventHandler extends EventHandler
{
	/**	 * Creates the appropriate event from a message passed to this method	 * @param the event object with the message needed to identify the behaviour required.	 */
	public void createEvent(CustomEvent event) 
	{	
		Designer designer = Designer.getInstance();
		if (event.getName().equals("presPrint")){
			PrinterJob pJob = PrinterJob.getPrinterJob();
			PageFormat format = pJob.defaultPage();
			pJob.setPrintable(Canvas.getInstance(), format);
			if(pJob.printDialog()){
				try{
					pJob.print();
				}catch(PrinterException pe){
					System.out.println(pe);
				}				return;
			}			return;
		}
		if (event.getName().equals("presExport")){			FileSaveDialog fsd = FileSaveDialog.getInstance(); //need to just instantiate the object so the next line does not return nulls
			JFileChooser dialog = FileSaveDialog.getDialog();			File selected = fsd.show();			if(selected != null){				if(FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.BMP)||						FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.GIF)||						FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.JPEG)||						FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.PNG)){					try {						String selectedFilterExt = FileExtensionUtils.getExtension(selected);						ExportImage.export(selected, selectedFilterExt);						Prescription.getInstance().setUnchanged();						return;					} catch (IOException e) {						e.printStackTrace();					}				}else if(FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.XML)){					String filename = selected.getPath();					ExportXML.export(filename);					Prescription.getInstance().setUnchanged();					return;				}else if(FileExtensionUtils.getExtension(dialog.getSelectedFile()).equals(FileExtensionUtils.CSS)){					String filename = selected.getPath();					ExportCSS.export(filename);					Prescription.getInstance().setUnchanged();					return;				}			}			return;
		}
		if (event.getName().equals("presImport")){
			designer.saveChanges(false);
			// if import is cancelled don't do anything
			if (ImportXML.importXML())
			{
				designer.switchLists(false);
				designer.setButtonsUsable();
				Prescription.getInstance().setUnchanged();				return;
			}			return;
		}
		if (event.getName().equals("back"))
		{
			if(designer.saveChanges(false))
			{
				Prescription.getInstance().clear();
				Toolbox.getInstance().resetToolbox();
				designer.switchLists(true);
				designer.update();				return;
			}			return;
		}
	}
}